package com.zlx.controller;

import com.zlx.constants.Constant;
import com.zlx.entities.Meeting;
import com.zlx.entities.User;
import com.zlx.service.MeetingService;
import com.zlx.utils.Page;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author 周罗校
 * @Description
 * @ClassName MeetingServlet
 * @date 2020.09.25 10:35:32
 * 该版权归周罗校所有，最终解释权以官方为准
 */

@WebServlet("/meeting/*")
public class MeetingServlet extends BaseServlet {
    private MeetingService meetingService = new MeetingService();

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        if (name == null) {
            name = "";
        }
        //获取当前页
        String currentPage = request.getParameter("currentPage");
        if (currentPage == null || currentPage == "") {
            currentPage = "1";
        }
        //使用分页工具类封装
        Page<Meeting> page = new Page<>(meetingService.getMeetCount(name), null, null, Integer.valueOf(currentPage));
        List<Meeting> list = meetingService.listAll(page, name);
        page.setItems(list);
        request.setAttribute(Constant.MEETING_LIST, page);
        request.setAttribute("name", name);
        request.getRequestDispatcher("/html/meeting/list.jsp").forward(request, response);
    }

    protected void insertMeeting(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map parameterMap = request.getParameterMap();
//        System.out.println(request.getParameter("makeUser"));
        Meeting meeting = new Meeting();
        try {
            BeanUtils.populate(meeting, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        meetingService.InsertMeeting(meeting);
        response.sendRedirect("/meeting/list");
       /* System.out.println(meeting);
        System.out.println(Arrays.toString(meeting.getMakeUsers()));*/
    }

    protected void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        if (id == null) {
            id = (String) request.getAttribute("id");
        }
        String uid = request.getParameter("uid");
        //查询会议详情
        Meeting meeting = meetingService.getMeetingInfoById(Integer.valueOf(id));
        //查询用户是已经否参加了会议
        Integer joinMeeting = meetingService.getJoinMeeting(Integer.valueOf(uid), Integer.valueOf(id));
        //获取实际参与会议的人数
        Integer joinMeetinngCount = meetingService.getJoinMeetinngCount(Integer.valueOf(id));
        //需要参与会议的人数
        Integer needMeetingCount = meeting.getMakeUser().split(",").length;
        //请求域带数据去前端
        request.setAttribute("needMeetingCount", needMeetingCount);
        request.setAttribute("joinMeetinngCount", joinMeetinngCount);
        request.setAttribute("joinMeeting", joinMeeting);
        request.setAttribute("meetingDetail", meeting);
        request.getRequestDispatcher("/html/meeting/detail.jsp").forward(request, response);
    }

    protected void joinMeeting(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uid = request.getParameter("uid");
        String mid = request.getParameter("mid");

        request.setAttribute("id", mid);
        meetingService.insertJoinMeetingInfo(Integer.valueOf(uid), Integer.valueOf(mid));
        request.getRequestDispatcher("/meeting/detail").forward(request, response);
    }

    protected void cancelJoinMeeting(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uid = request.getParameter("uid");
        String mid = request.getParameter("mid");

        request.setAttribute("id", mid);
        meetingService.deleteJoinMeeting(Integer.valueOf(uid), Integer.valueOf(mid));
        request.getRequestDispatcher("/meeting/detail").forward(request, response);
    }
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String currentPage = request.getParameter("currentPage");
        request.setAttribute("name",name);
        request.setAttribute("currentPage",currentPage);
        meetingService.deleteMeetinngById(Integer.valueOf(id));
        request.getRequestDispatcher("/meeting/list").forward(request,response);
    }
}
